﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraTab;
using DevExpress.XtraTab.ViewInfo;
using System.Drawing;
using DevExpress.XtraEditors;


namespace YJY_DMZS
{
    public partial class FrmMainRibbon : RibbonForm
    {
        public FrmMainRibbon()
        {
            InitializeComponent();

            SkinHelper.InitSkinGallery(rgbiSkins, true);
            ribbonControl.Minimized = true;
            if (System.IO.File.Exists("logo.png"))
            {
                ribbonControl.ApplicationIcon = new System.Drawing.Bitmap("logo.png");
            }
        }

        private void FrmMainRibbon_Load(object sender, EventArgs e)
        {
            FrmNavigation frm = new FrmNavigation();
            frm.MdiParent = this;
            frm.MaximizeBox = false;
            frm.MinimizeBox = false;
            frm.CloseBox = false;
            frm.Show();
        }


        #region 导航按钮事件

        #region 基础档案
        //用户
        private void nbiUser_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.BasisFile.FrmUser";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }

        //产品类型
        private void nbiProductType_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.BasisFile.FrmProductType";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }

        //产品
        private void nbiProduct_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.BasisFile.FrmProduct";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }

        //部门类型
        private void nbiDepartmentType_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.BasisFile.FrmDepartmentType";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }

        //部门
        private void nbiDepartment_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.BasisFile.FrmDepartmentNew";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }

        //计量单位
        private void nbiUnitBase_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.BasisFile.FrmUnitBase";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }

        //员工
        private void nbiEmployee_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.BasisFile.FrmEmployee";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }

        //区域
        private void nbiArea_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.BasisFile.FrmArea";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }

        //客户
        private void nbiClient_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.BasisFile.FrmClient";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }

        //供应商
        private void nbiSupplier_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.BasisFile.FrmSupplier";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }

        //仓库
        private void nbiWarehouse_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.BasisFile.FrmWarehouse";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }
        #endregion

        #region 供应链
        //采购订单
        private void nbiPurchaseOrder_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.SupplyChain.FrmPurchaseOrder";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }

        //销售订单
        private void nbiSalesOrder_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.SupplyChain.FrmSalesOrder";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }

        //入库
        private void nbiPurchaseIn_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.SupplyChain.FrmInStock";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, false);
        }

        //出库
        private void nbiOutStock_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.SupplyChain.FrmOutStock";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, false);
        }
        #endregion

        #region 条码
        //条码生成
        private void nbiBarcodeCreate_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.Barcode.FrmBarcodeCreate";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }

        //条码追查       
        private void nbiBarcodeTrace_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string assemblyName = "YJY_DMZS";
            string fullName = "YJY_DMZS.Barcode.FrmBarcodeTrace";

            CreateInstance((sender as DevExpress.XtraNavBar.NavBarItem), assemblyName, fullName, true);
        }
        #endregion

        #endregion


        /// <summary>
        /// 实例化窗口
        /// </summary>
        /// <param name="sender">不是NavBarItem，传null</param>
        /// <param name="assemblyName">程序集</param>
        /// <param name="fullName">类全名</param>
        /// <param name="single">单开</param>
        private void CreateInstance(DevExpress.XtraNavBar.NavBarItem sender, string assemblyName, string fullName, bool single)
        {
            Type type = System.Reflection.Assembly.Load(assemblyName).GetType(fullName, false, false);

            if (type == null)
            {
                XtraMessageBox.Show("窗体类名更改，编写人员请查看主窗体的方法 CreateInstance ", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (single)
            {
                foreach (Form form in Application.OpenForms)
                {
                    if (form.GetType().FullName == fullName)
                    {
                        form.Activate();
                        return;
                    }
                }
            }

            var frm = Activator.CreateInstance(type) as Form;

            if (sender != null)
            {
                int i = (sender as DevExpress.XtraNavBar.NavBarItem).SmallImageIndex;

                if (i != -1 && i < navbarImageList.Images.Count)
                {
                    var img = navbarImageList.Images[(sender as DevExpress.XtraNavBar.NavBarItem).SmallImageIndex];
                    System.IO.MemoryStream stream = new System.IO.MemoryStream();
                    img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    var icon = Icon.FromHandle(new Bitmap(stream).GetHicon());
                    stream.Close();
                    stream.Dispose();
                    frm.Icon = icon;
                }
            }
            frm.MdiParent = this;
            frm.Show();
        }

        public void CreateInstance(string assemblyName, string fullName, bool single)
        {
            CreateInstance(null, assemblyName, fullName, single);
        }

        private void bbiHomePage_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (Form form in Application.OpenForms)
            {
                if (form.GetType().FullName == "YJY_DMZS.FrmNavigation")
                {
                    form.Activate();
                    return;
                }
            }

            FrmNavigation frm = new FrmNavigation();
            frm.MdiParent = this;
            frm.MaximizeBox = false;
            frm.MinimizeBox = false;
            frm.CloseBox = false;
            frm.Show();
        }



    }
}
